package com.huaqin.obeacons.bluetooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import com.huaqin.obeacons.R;

public class BluetoothUtils {
	private final static String TAG = "BluetoothUtils";
	private static BluetoothAdapter mBluetoothAdapter;

	public static void initBluetooth(Context context) {
		Log.d(TAG, "initBluetooth");
		// Use this check to determine whether BLE is supported on the device.
		// Then you can
		// selectively disable BLE-related features.
		if (!context.getPackageManager().hasSystemFeature(
				PackageManager.FEATURE_BLUETOOTH_LE)) {
			Toast.makeText(context, R.string.ble_not_supported,
					Toast.LENGTH_SHORT).show();
		}

		// Initializes a Bluetooth adapter. For API level 18 and above, get a
		// reference to
		// BluetoothAdapter through BluetoothManager.
		final BluetoothManager bluetoothManager = (BluetoothManager) context
				.getSystemService(Context.BLUETOOTH_SERVICE);
		mBluetoothAdapter = bluetoothManager.getAdapter();
		// Checks if Bluetooth is supported on the device.
		if (mBluetoothAdapter == null) {
			Toast.makeText(context, R.string.error_bluetooth_not_supported,
					Toast.LENGTH_SHORT).show();
		}
		// Ensures Bluetooth is enabled on the device. If Bluetooth is not
		// currently enabled,
		// fire an intent to display a dialog asking the user to grant
		// permission to enable it.
		if (!mBluetoothAdapter.isEnabled()) {
			// 通知用户请求开启蓝牙
			// Intent intent = new
			// Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// context.startActivity(intent);
			// 系统自动开启蓝牙
			mBluetoothAdapter.enable();
		}
	}

	public static String getDeviceName(String uuid) {
		String name = new String("不合法的设备");;
		Log.d(TAG,"getDeviceName uuid= "+uuid);
		if (uuid.equals("e2c56db5-dffb-48d2-b060-d0f5a71096e0")) {
			name = new String("9号楼考勤机");
		} else if (uuid.equals("XXXXXXXXXXXXXXXXXXXXXXX")) {
			name = new String("1号楼考勤机");
		} else if (uuid.equals("XXXXXXXXXXXXXXXXXXXXXXX")) {
			name = new String("12号楼考勤机");
		} else if (uuid.equals("XXXXXXXXXXXXXXXXXXXXXXX")) {
			name = new String("华虹考勤机");
		}
		Log.d(TAG,"getDeviceName name= "+name);
		return name;
	}
}
